********************************************************************************
* Fight or Flight: Endogenous timing in conflicts ******************************
* Boris van Leeuwen, Theo Offerman & Jeroen van de Ven *************************
* June 26, 2020 ****************************************************************
********************************************************************************

clear
cd "C:\Users\bleeuwen\Dropbox\Fight or flight\Data\analysis"

*******************************
* Data preperation ************
*******************************

* Import the data
import delimited "fof_subjects.csv", clear
save "fof_subjects.dta", replace

import delimited "fof_results.csv", clear
save "fof_results.dta", replace

use "fof_subjects.dta", clear

gen groupid=10*session+mgroep

encode treatment, gen(timing) 
label define timing_value 1 "Dynamic" 2 "Dynamic (exp. 2)" 3 "Static"
label values timing timing_value

label define det_value -6 "k = -6" 6 "k = 6" 12 "k = 12"
label values k det_value

gen female=.
replace female=1 if sexe=="vrouw"
replace female=0 if sexe=="man"

forvalues k = 1(1)18 {
replace q`k'=. if q`k'==0
}

gen dominance=(q3+q5+q7+q9+(8-q10)+q11+q12+q16)/8
gen prestige=(q1+(8-q2)+q4+(8-q6)+q8+q13+q14+q15+q17)/9

gen accepta=.
gen acceptb=.
gen acceptc=.
gen acceptd=.
gen accepte=.
gen acceptf=.

replace accepta=0 if lotterya=="reject"
replace acceptb=0 if lotteryb=="reject"
replace acceptc=0 if lotteryc=="reject"
replace acceptd=0 if lotteryd=="reject"
replace accepte=0 if lotterye=="reject"
replace acceptf=0 if lotteryf=="reject"

replace accepta=1 if lotterya=="accept"
replace acceptb=1 if lotteryb=="accept"
replace acceptc=1 if lotteryc=="accept"
replace acceptd=1 if lotteryd=="accept"
replace accepte=1 if lotterye=="accept"
replace acceptf=1 if lotteryf=="accept"

gen lotteries_accepted = accepta + acceptb + acceptc + acceptd + accepte + acceptf
gen lotteries_rejected = 6 - lotteries_accepted

replace contest=. if lotteries_rejected==.

drop accepta acceptb acceptc acceptd accepte acceptf lotteries_accepted

label variable lotteries_rejected "Risk aversion"
egen z_dominance=std(dominance)
egen z_prestige=std(prestige)
egen z_strength=std(strength)
egen z_masculinity=std(masculinity)
egen z_jow_sr=std(q18)
label variable female "Female"
label variable z_dominance "Dominance"
label variable z_prestige "Prestige"
label variable z_strength "Physical strength"
label variable z_masculinity "Masculinity"
label variable contest "Joy of winning (incentivized)"
label variable z_jow_sr "Joy of winning (self-report)"
label variable mistakes "Mistakes instr."

merge 1:m subjectid using "fof_results.dta"
drop if _merge==1
drop _merge

gen tijd2=tijd
replace tijd2=10000 if tijd>10000 & timing==1 
replace tijd2=11000 if timing==1 & decision=="exo"

gen tijd3=tijd
replace tijd3=10000 if tijd>10000 & timing==1
replace tijd3=10000 if timing==1 & decision=="exo"

gen period=.
forvalues k = 1(1)50 {
replace period=`k' if tijd2>(`k'-1)*200 & tijd2<=`k'*200 & timing==1
}
replace period=50 if tijd2>10000 & tijd2<11000 & timing==1
replace period=51 if tijd2==11000 & timing==1

gen choosefight=0
replace choosefight=1 if mymove=="fight"
replace choosefight=. if mymove=="oth_inactiv"

gen chooseflight=0
replace chooseflight=1 if mymove=="flight"
replace chooseflight=. if mymove=="oth_inactiv"


* tie=0 indicates that one player was waiting when the other moved
* tie=1 indicates that the other moved in a later (time) period
* tie=2 indicates that the other moved in an earlier (time) period
* tie=3 indicates that both moved in the same (time) period
gen cf=choosefight
replace cf=0 if tie==2
gen cflee=chooseflight
replace cflee=0 if tie==2

forvalues k = 1(1)11 {
replace second=`k' if tijd2>(`k'-1)*1000 & tijd2<=`k'*1000 & timing==1 
}

gen fight_first_second=0
replace fight_first_second=1 if choosefight==1 & second==1 
replace fight_first_second=. if mymove=="oth_inactiv"

gen flee_before_end=0
replace flee_before_end=1 if chooseflight==1 & second<11 & timing==1 
replace flee_before_end=1 if chooseflight==1 & timeperiod<4 & timing==2 
replace flee_before_end=. if mymove=="oth_inactiv"

gen fight_first_period=0
replace fight_first_period=1 if choosefight==1 & period==1 & timing==1
replace fight_first_period=1 if choosefight==1 & timeperiod==1 & timing==2
replace fight_first_period=. if mymove=="oth_inactiv"

gen ability_01 = ability/1000
label variable ability_01 "Fighting ability"
label variable ability "Ability"
label variable round "Round"
label variable flee_before_end "Flee before endgame"

gen battle=.
replace battle=0 if verdiensten ==0
replace battle=0 if verdiensten == 10+k
replace battle=1 if verdiensten == 10
replace battle=1 if verdiensten == -10
replace battle=. if mymove=="oth_inactiv"

gen battle_ties=.
replace battle_ties=1 if mymove=="fight" & othermove=="none"
replace battle_ties=1 if mymove=="none" & othermove=="fight"
replace battle_ties=1 if mymove=="fight" & othermove=="fight" 
replace battle_ties=0 if mymove=="flight" & othermove=="none"
replace battle_ties=0 if mymove=="none" & othermove=="flight"
replace battle_ties=0 if mymove=="flight" & othermove=="flight"
replace battle_ties=0 if mymove=="fight" & othermove=="flight" & canflight==1
replace battle_ties=0 if mymove=="flight" & othermove=="fight" & canflight==1
replace battle_ties=1 if mymove=="fight" & othermove=="flight" & canflight==0
replace battle_ties=1 if mymove=="flight" & othermove=="fight" & canflight==0

gen stronger=.
replace stronger=1 if ability>=ability_other
replace stronger=0 if ability<ability_other

gen sorting = .
replace sorting = 1 if stronger == 0 & verdiensten == 0
replace sorting = 1 if stronger == 1 & verdiensten == 10+k
replace sorting = 0 if stronger == 0 & verdiensten != 0
replace sorting = 0 if stronger == 1 & verdiensten != 10+k
replace sorting = 0 if verdiensten == 10
replace sorting = 0 if verdiensten == -10
replace sorting=. if mymove=="oth_inactiv"
label variable sorting "Weaker escapes"

gen action_flight=0
replace action_flight=1 if mymove=="flight" & decision=="endo" & timing==1
replace action_flight=1 if mymove=="flight" & timeperiod<4 & timing==2
replace action_flight=1 if mymove=="flight" & timing==3
replace action_flight=. if mymove=="oth_inactiv"
gen action_wait=0
replace action_wait=1 if decision=="exo" & timing==1
replace action_wait=1 if mymove!="none" & timeperiod==4 & timing==2
replace action_wait=. if mymove=="oth_inactiv"
gen action_other=0
replace action_other=1 if mymove=="none" & decision=="endo"
replace action_other=. if mymove=="oth_inactiv"
gen action_fight=0
replace action_fight=1 if mymove=="fight" & decision=="endo" & timing==1
replace action_fight=1 if mymove=="fight" & timeperiod<4 & timing==2
replace action_fight=1 if mymove=="fight" & timing==3
replace action_fight=. if mymove=="oth_inactiv"

gen q100=.
replace q100=1 if ability<=100
replace q100=2 if ability>100 & ability<=200
replace q100=3 if ability>200 & ability<=300
replace q100=4 if ability>300 & ability<=400
replace q100=5 if ability>400 & ability<=500
replace q100=6 if ability>500 & ability<=600
replace q100=7 if ability>600 & ability<=700
replace q100=8 if ability>700 & ability<=800
replace q100=9 if ability>800 & ability<=900
replace q100=10 if ability>900 & ability<=1000

*stata does not accept negative factor variables in regressions, so normalize...
gen k_norm=k+10
label define det_value2 4 "k = -6" 16 "k = 6" 22 "k = 12"
label values k_norm det_value2

save "fof.dta", replace


* Merge decisions of others

use "fof.dta", clear 
keep subjectid round mymove period timeperiod
rename * *_other
rename round_other round
save "other.dta",replace
use "fof.dta", clear
gen subjectid_other=100*session+ppother
joinby subjectid_other round using "other.dta", unmatched(none)
gen action_period = .
replace action_period = min(period,period_other) if timing==1
replace action_period = min(timeperiod,timeperiod_other) if timing==2
save "fof.dta", replace



